package com.ling.pl.core.commons.utils;

import com.ling.pl.core.commons.exception.BusinessException;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import java.io.*;
import java.math.BigInteger;
import java.nio.ByteBuffer;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
import java.security.MessageDigest;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.text.DecimalFormat;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;

public class FileUtils {

    public final static String UTF8_ENCODING = "UTF-8";
    public final static String ISO_ENCODING = "ISO-8859-1";
    public final static String ASC_ENCODING = "ASCII";
    public final static String UTF16_ENCODING = "UTF-16";
    public final static String GBK_ENCODING = "GBK";
    public final static String GB2312_ENCODING = "GB2312";
    private static final Log log = LogFactory.getLog(FileUtils.class);
    public static String SEPARATOR = File.separator;

    /**
     * 确保目录存在，不存在就递归创建
     *
     * @param destination 必须是文件路径而不是文件夹
     */
    public static void ensureExistence(File destination) {
        // if the directory exists, make sure it is a directory
        File dir = destination.getAbsoluteFile().getParentFile();
        if (dir.exists() && !dir.isDirectory()) {
            throw new BusinessException("The path: " + dir.getAbsolutePath()
                    + " exists, but is not a directory");
        } // else make the directory and any non-existent parent directories
        else if (!dir.exists()) {
            if (!dir.mkdirs()) {
                throw new BusinessException("unable to create directory: "
                        + dir.getAbsolutePath());
            }
        }
    }

    /**
     * 确保目录存在，不存在就递归创建
     *
     * @param destination 必须是文件路径而不是文件夹
     */
    public static void ensureExistence(String destination) {
        ensureExistence(new File(destination));
    }

    public static String getFileName(String filepath) {
        File file = new File(filepath);
        String filename = file.getName();
        int dotindex = filename.lastIndexOf(".");
        if (dotindex > -1) {
            return filename.substring(0, dotindex);
        }
        return filename;
    }

    public static String getFileType(String filepath) {
        File file = new File(filepath);
        String filename = file.getName();
        if (filename.lastIndexOf(".") > 0) {
            return filename.substring(filename.lastIndexOf(".") + 1);
        }
        return "";

    }

    /**
     * 确保目录以/结尾
     *
     * @param folder
     * @return
     */
    public static String ensureFolderEnd(String folder) {
        if (!folder.endsWith(SEPARATOR)) {
            folder = folder + SEPARATOR;
        }
        return folder;
    }

    /**
     * 获取文件MD5值
     *
     * @param file
     * @return
     */
    public static String getMd5ByFile(File file) {
        String value = null;
        FileInputStream in = null;
        try {
            in = new FileInputStream(file);
            MappedByteBuffer byteBuffer = in.getChannel().map(
                    FileChannel.MapMode.READ_ONLY, 0, file.length());
            MessageDigest md5 = MessageDigest.getInstance("MD5");
            md5.update(byteBuffer);
            BigInteger bi = new BigInteger(1, md5.digest());
            value = bi.toString(16);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (null != in) {
                try {
                    in.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return value;
    }

    /**
     * 获取文件大小
     *
     * @param file
     * @return
     */
    public static long getFileLength(File file) throws IOException {
        FileInputStream fis = null;
        fis = new FileInputStream(file);
        return fis.available();
    }

    /**
     * 读取文件到二进制
     *
     * @param file
     * @return
     * @throws IOException
     * @author WikerYong Email:<a href="#">yw_312@foxmail.com</a>
     * @version 2012-3-23 上午11:47:06
     */
    public static byte[] getBytesFromFile(File file) throws IOException {
        InputStream is = new FileInputStream(file);

        long length = file.length();

        if (length > Integer.MAX_VALUE) {
            // File is too large
        }

        byte[] bytes = new byte[(int) length];

        // Read in the bytes
        int offset = 0;
        int numRead = 0;
        while (offset < bytes.length
                && (numRead = is.read(bytes, offset, bytes.length - offset)) >= 0) {
            offset += numRead;
        }

        // Ensure all the bytes have been read in
        if (offset < bytes.length) {
            throw new IOException("不能读取文件: " + file.getName());
        }

        is.close();
        return bytes;
    }

    /**
     * 获取标准文件大小，如30KB，15.5MB
     *
     * @param file
     * @return
     * @throws IOException
     */
    public static String getFileSize(File file) throws IOException {
        long size = getFileLength(file);
        DecimalFormat df = new DecimalFormat("###.##");
        float f;
        if (size < 1024 * 1024) {
            f = (float) ((float) size / (float) 1024);
            return (df.format(new Float(f).doubleValue()) + " KB");
        } else {
            f = (float) ((float) size / (float) (1024 * 1024));
            return (df.format(new Float(f).doubleValue()) + " MB");
        }

    }

    /**
     * 复制文件
     *
     * @param f1 源文件
     * @param f2 目标文件
     * @throws Exception
     */
    public static void copyFile(File f1, File f2) throws Exception {
        int length = 2097152;
        FileInputStream in = new FileInputStream(f1);
        FileOutputStream out = new FileOutputStream(f2);
        FileChannel inC = in.getChannel();
        FileChannel outC = out.getChannel();
        ByteBuffer b = null;
        while (true) {
            if (inC.position() == inC.size()) {
                inC.close();
                outC.close();
            }
            if ((inC.size() - inC.position()) < length) {
                length = (int) (inC.size() - inC.position());
            } else
                length = 2097152;
            b = ByteBuffer.allocateDirect(length);
            inC.read(b);
            b.flip();
            outC.write(b);
            outC.force(false);
        }
    }

    /**
     * 检查文件是否存在
     *
     * @param fileName
     * @return
     * @throws IOException
     */
    public static boolean existFile(String fileName) throws IOException {
        File file = new File(fileName);
        if (!file.exists()) {
            throw new IOException("文件未找到:" + fileName);
        }
        return file.exists();
    }

    /**
     * 删除文件
     *
     * @param fileName
     */
    public static void deleteFile(String fileName) throws IOException {
        File file = new File(fileName);
        if (!file.exists()) {
            throw new IOException("文件未找到:" + fileName);
        }
        file.delete();
    }

    /**
     * 读取文件到字符串
     *
     * @param fileName
     * @return
     * @throws IOException
     */
    public static String readFile(String fileName) throws IOException {
        File file = new File(fileName);
        if (!file.exists()) {
            throw new IOException("文件未找到:" + fileName);
        }
        return readFile(file);
    }

    public static String readFile(File file) throws IOException {
        BufferedReader in = new BufferedReader(new FileReader(file));
        StringBuffer sb = new StringBuffer();
        String str = "";
        while ((str = in.readLine()) != null) {
            sb.append(str);
        }
        in.close();
        return sb.toString();
    }

    /**
     * 获取目录所有所有文件和文件夹
     *
     * @param fileName
     * @return
     * @throws IOException
     */
    public static List<File> listFiles(String fileName) throws IOException {
        File file = new File(fileName);
        if (!file.exists()) {
            throw new IOException("文件未找到:" + fileName);
        }
        return Arrays.asList(file.listFiles());
    }

    /**
     * 创建目录
     *
     * @param dir
     */
    public static void mkdir(String dir) {
        String dirTemp = dir;
        File dirPath = new File(dirTemp);
        if (!dirPath.exists()) {
            dirPath.mkdir();
        }
    }

    /**
     * 新建文件
     *
     * @param fileName String 包含路径的文件名 如:E:\phsftp\src\123.txt
     * @param content  String 文件内容
     */
    public static void createNewFile(String fileName, String content)
            throws IOException {
        String fileNameTemp = fileName;
        File filePath = new File(fileNameTemp);
        if (!filePath.exists()) {
            filePath.createNewFile();
        }
        FileWriter fw = new FileWriter(filePath);
        PrintWriter pw = new PrintWriter(fw);
        String strContent = content;
        pw.println(strContent);
        pw.flush();
        pw.close();
        fw.close();

    }

    /**
     * 删除文件夹
     *
     * @param folderPath 文件夹路径
     */
    public static void delFolder(String folderPath) {
        // 删除文件夹里面所有内容
        delAllFile(folderPath);
        String filePath = folderPath;
        File myFilePath = new File(filePath);
        // 删除空文件夹
        myFilePath.delete();
    }

    /**
     * 删除文件夹里面的所有文件
     *
     * @param path 文件夹路径
     */
    public static void delAllFile(String path) {
        File file = new File(path);
        if (!file.exists()) {
            return;
        }
        if (!file.isDirectory()) {
            return;
        }
        String[] childFiles = file.list();
        File temp = null;
        for (int i = 0; i < childFiles.length; i++) {
            // File.separator与系统有关的默认名称分隔符
            // 在UNIX系统上，此字段的值为'/'；在Microsoft Windows系统上，它为 '\'。
            if (path.endsWith(File.separator)) {
                temp = new File(path + childFiles[i]);
            } else {
                temp = new File(path + File.separator + childFiles[i]);
            }
            if (temp.isFile()) {
                temp.delete();
            }
            if (temp.isDirectory()) {
                delAllFile(path + File.separatorChar + childFiles[i]);// 先删除文件夹里面的文件
                delFolder(path + File.separatorChar + childFiles[i]);// 再删除空文件夹
            }
        }
    }

    /**
     * 复制单个文件，传统方式
     *
     * @param srcFile 包含路径的源文件 如：E:/phsftp/src/abc.txt
     * @param dirDest 目标文件目录；若文件目录不存在则自动创建 如：E:/phsftp/dest
     * @throws IOException
     */
    public static void copyFileToFolder(String srcFile, String dirDest)
            throws IOException {
        FileInputStream in = new FileInputStream(srcFile);
        mkdir(dirDest);
        FileOutputStream out = new FileOutputStream(dirDest + "/"
                + new File(srcFile).getName());
        int len;
        byte buffer[] = new byte[1024];
        while ((len = in.read(buffer)) != -1) {
            out.write(buffer, 0, len);
        }
        out.flush();
        out.close();
        in.close();
    }

    /**
     * 复制单个文件，传统方式
     *
     * @param srcFile    包含路径的源文件 如：E:/phsftp/src/abc.txt
     * @param targetFile 目标文件目录；若文件目录不存在则自动创建 如：E:/phsftp/dest
     * @throws IOException
     */
    public static void copyFile(String srcFile, String targetFile)
            throws IOException {
        FileInputStream in = new FileInputStream(srcFile);
        ensureExistence(targetFile);
        FileOutputStream out = new FileOutputStream(targetFile);
        int len;
        byte buffer[] = new byte[1024];
        while ((len = in.read(buffer)) != -1) {
            out.write(buffer, 0, len);
        }
        out.flush();
        out.close();
        in.close();
    }

    /**
     * 复制文件夹
     *
     * @param oldPath String 源文件夹路径 如：E:/phsftp/src
     * @param newPath String 目标文件夹路径 如：E:/phsftp/dest
     * @return boolean
     */
    public static void copyFolder(String oldPath, String newPath)
            throws IOException {
        // 如果文件夹不存在 则新建文件夹
        mkdir(newPath);
        File file = new File(oldPath);
        String[] files = file.list();
        File temp = null;
        for (int i = 0; i < files.length; i++) {
            if (oldPath.endsWith(File.separator)) {
                temp = new File(oldPath + files[i]);
            } else {
                temp = new File(oldPath + File.separator + files[i]);
            }

            if (temp.isFile()) {
                FileInputStream input = new FileInputStream(temp);
                FileOutputStream output = new FileOutputStream(newPath + "/"
                        + (temp.getName()).toString());
                byte[] buffer = new byte[1024 * 2];
                int len;
                while ((len = input.read(buffer)) != -1) {
                    output.write(buffer, 0, len);
                }
                output.flush();
                output.close();
                input.close();
            }
            if (temp.isDirectory()) {// 如果是子文件夹
                copyFolder(oldPath + "/" + files[i], newPath + "/" + files[i]);
            }
        }
    }

    /**
     * 移动文件到指定目录
     *
     * @param oldPath 包含路径的文件名 如：E:/phsftp/src/ljq.txt
     * @param newPath 目标文件目录 如：E:/phsftp/dest
     */
    public static void moveFile(String oldPath, String newPath)
            throws IOException {
        copyFileToFolder(oldPath, newPath);
        deleteFile(oldPath);
    }

    /**
     * 移动文件到指定目录，不会删除文件夹
     *
     * @param oldPath 源文件目录 如：E:/phsftp/src
     * @param newPath 目标文件目录 如：E:/phsftp/dest
     */
    public static void moveFiles(String oldPath, String newPath)
            throws IOException {
        copyFolder(oldPath, newPath);
        delAllFile(oldPath);
    }

    /**
     * 移动文件到指定目录，会删除文件夹
     *
     * @param oldPath 源文件目录 如：E:/phsftp/src
     * @param newPath 目标文件目录 如：E:/phsftp/dest
     */
    public static void moveFolder(String oldPath, String newPath)
            throws IOException {
        copyFolder(oldPath, newPath);
        delFolder(oldPath);
    }

    /**
     * 解压zip文件 说明:本程序通过ZipOutputStream和ZipInputStream实现了zip压缩和解压功能.
     * 问题:由于java.util.zip包并不支持汉字,当zip文件中有名字为中文的文件时,
     * 就会出现异常:"Exception  in thread "main " java.lang.IllegalArgumentException
     * at java.util.zip.ZipInputStream.getUTF8String(ZipInputStream.java:285)
     *
     * @param srcDir  解压前存放的目录
     * @param destDir 解压后存放的目录
     * @throws Exception
     */
    public static void unZip(String srcDir, String destDir) throws IOException {
        int leng = 0;
        byte[] b = new byte[1024 * 2];
        /** 获取zip格式的文件 **/
        File[] zipFiles = new ExtensionFileFilter("zip").getFiles(srcDir);
        if (zipFiles != null && zipFiles.length > 0) {
            for (int i = 0; i < zipFiles.length; i++) {
                File file = zipFiles[i];
                /** 解压的输入流 * */
                ZipInputStream zis = new ZipInputStream(new FileInputStream(
                        file));
                ZipEntry entry = null;
                while ((entry = zis.getNextEntry()) != null) {
                    File destFile = null;
                    if (destDir.endsWith(File.separator)) {
                        destFile = new File(destDir + entry.getName());
                    } else {
                        destFile = new File(destDir + File.separator
                                + entry.getName());
                    }
                    /** 把解压包中的文件拷贝到目标目录 * */
                    FileOutputStream fos = new FileOutputStream(destFile);
                    while ((leng = zis.read(b)) != -1) {
                        fos.write(b, 0, leng);
                    }
                    fos.close();
                }
                zis.close();
            }
        }
    }

    /**
     * 压缩文件 说明:本程序通过ZipOutputStream和ZipInputStream实现了zip压缩和解压功能.
     * 问题:由于java.util.zip包并不支持汉字,当zip文件中有名字为中文的文件时,
     * 就会出现异常:"Exception  in thread "main " java.lang.IllegalArgumentException
     * at java.util.zip.ZipInputStream.getUTF8String(ZipInputStream.java:285)
     *
     * @param srcDir  压缩前存放的目录
     * @param destDir 压缩后存放的目录
     * @throws Exception
     */
    public static void zip(String srcDir, String destDir) throws IOException {
        String tempFileName = null;
        byte[] buf = new byte[1024 * 2];
        int len;
        // 获取要压缩的文件
        File[] files = new File(srcDir).listFiles();
        if (files != null) {
            for (File file : files) {
                if (file.isFile()) {
                    FileInputStream fis = new FileInputStream(file);
                    BufferedInputStream bis = new BufferedInputStream(fis);
                    if (destDir.endsWith(File.separator)) {
                        tempFileName = destDir + file.getName() + ".zip";
                    } else {
                        tempFileName = destDir + File.separator
                                + file.getName() + ".zip";
                    }
                    FileOutputStream fos = new FileOutputStream(tempFileName);
                    BufferedOutputStream bos = new BufferedOutputStream(fos);
                    ZipOutputStream zos = new ZipOutputStream(bos);// 压缩包

                    ZipEntry ze = new ZipEntry(file.getName());// 压缩包文件名
                    zos.putNextEntry(ze);// 写入新的ZIP文件条目并将流定位到条目数据的开始处

                    while ((len = bis.read(buf)) != -1) {
                        zos.write(buf, 0, len);
                        zos.flush();
                    }
                    bis.close();
                    zos.close();

                }
            }
        }
    }

    /**
     * 读取数据
     *
     * @param inSream
     * @param charsetName
     * @return
     * @throws Exception
     */
    public static String readData(InputStream inSream, String charsetName)
            throws IOException {
        ByteArrayOutputStream outStream = new ByteArrayOutputStream();
        byte[] buffer = new byte[1024];
        int len = -1;
        while ((len = inSream.read(buffer)) != -1) {
            outStream.write(buffer, 0, len);
        }
        byte[] data = outStream.toByteArray();
        outStream.close();
        inSream.close();
        return new String(data, charsetName);
    }

    /**
     * 一行一行读取文件，适合字符读取，若读取中文字符时会出现乱码
     *
     * @param path
     * @return
     * @throws Exception
     */
    public static Set<String> readFileLine(String path) throws IOException {
        Set<String> datas = new HashSet<String>();
        FileReader fr = new FileReader(path);
        BufferedReader br = new BufferedReader(fr);
        String line = null;
        while ((line = br.readLine()) != null) {
            datas.add(line);
        }
        br.close();
        fr.close();
        return datas;
    }

//	public static void main(String[] args) {
//		try {
//			unZip("c:/test", "c:/test");
//		} catch (IOException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}
//	}

    /**
     * 获取系统的目录分隔符
     *
     * @return
     */
    public static String getSeparator() {
        return SEPARATOR;
    }

    public static void close(PrintWriter input) {
        if (input != null) {
            try {
                input.close();
            } catch (Exception e) {
                log.error(e.toString());
            }
        }
    }

    public static void close(BufferedReader input) {
        if (input != null) {
            try {
                input.close();
            } catch (Exception e) {
                log.error(e.toString());
            }
        }
    }

    public static void close(InputStreamReader input) {
        if (input != null) {
            try {
                input.close();
            } catch (Exception e) {
                log.error(e.toString());
            }
        }
    }

    public static void safeClose(InputStream input) {
        if (input != null) {
            try {
                input.close();
            } catch (Exception e) {
                log.error(e.toString());
            }
        }
    }

    public static void safeClose(PrintWriter pw) {
        if (pw != null) {
            try {
                pw.close();
            } catch (Exception e) {
                log.error(e.toString());
            }
        }
    }

    public static void safeClose(BufferedWriter bw) {
        if (bw != null) {
            try {
                bw.close();
            } catch (Exception e) {
                log.error(e.toString());
            }
        }
    }

    public static void safeClose(Reader reader) {
        if (reader != null) {
            try {
                reader.close();
            } catch (Exception e) {
                log.error(e.toString());
            }
        }
    }

    public static void safeClose(OutputStream out) {
        if (out != null) {
            try {
                out.close();
            } catch (Exception e) {
                log.error(e.toString());
            }
        }
    }

    public static void safeClose(OutputStreamWriter out) {
        if (out != null) {
            try {
                out.close();
            } catch (Exception e) {
                log.error(e.toString());
            }
        }
    }

    public static void safeClose(FileOutputStream out) {
        if (out != null) {
            try {
                out.close();
            } catch (Exception e) {
                log.error(e.toString());
            }
        }
    }

    public static void safeClose(ResultSet rs) {
        if (rs != null) {
            try {
                rs.close();
            } catch (Exception e) {
                log.error(e.toString());
            }
        }
    }

    public static void safeClose(Connection conn) {
        if (conn != null) {
            try {
                conn.close();
            } catch (Exception e) {
                log.error(e.toString());
            }
        }
    }

    public static void safeClose(PreparedStatement stmt) {
        if (stmt != null) {
            try {
                stmt.close();
            } catch (Exception e) {
                log.error(e.toString());
            }
        }
    }

    public static String getResource(Class defClass, String file) {
        StringBuilder result = new StringBuilder();
        BufferedReader in = null;
        try {
            in = new BufferedReader(new InputStreamReader(defClass
                    .getResourceAsStream(file)));
            String line;
            while ((line = in.readLine()) != null) {
                result.append(line + "\n");
            }
        } catch (IOException e) {
            throw new IllegalStateException("Reading standard css", e);
        } finally {
            if (in != null) {
                try {
                    in.close();
                } catch (IOException e) {
                    throw new IllegalStateException("Reading standard css", e);
                }
            }
        }
        return result.toString();
    }


    /**
     * 〈一句话功能简述〉
     * 功能详细描述
     *
     * @param dir
     * @see [相关类/方法]（可选）
     * @since [产品/模块版本] （可选）
     */

    public static void removeDirectory(File dir) throws IOException {
        if (!dir.isDirectory()) {
            return;
        }

        clearDirectory(dir);
        if (!dir.delete()) {
            throw new IOException("Can not delete \"" + dir.getAbsolutePath()
                    + "\".");
        }
    }

    public static void clearDirectory(File dir) throws IOException {
        if (!dir.isDirectory()) {
            return;
        }

        for (File subFile : dir.listFiles()) {
            if (subFile.isFile()) {
                if (!subFile.delete()) {
                    throw new IOException("Can not delete \""
                            + subFile.getAbsolutePath() + "\".");
                }
            } else if (subFile.isDirectory()) {
                removeDirectory(subFile);
            }
        }
    }

    public static String filerFileName(String str) throws PatternSyntaxException {
        // 只允许字母和数字
        // String regEx = "[^a-zA-Z0-9]";
        // 清除掉所有特殊字符
        String regEx = "[`~!@#$%^&*()+=|{}':;',//[//].<>/?~！@#￥%……&*（）——+|{}【】‘；：”“’。，、？]";
        Pattern p = Pattern.compile(regEx);
        Matcher m = p.matcher(str);
        return m.replaceAll("").trim();
    }

    public static String getFileContentByClassLocation(Class class1, String path) {
        InputStream in = class1.getResourceAsStream(path);//"request_queryjsp.xml"
        StringBuffer request = new StringBuffer();
        try {
            BufferedReader br = new BufferedReader(new InputStreamReader(in, "UTF-8"));
            try {
                String line = br.readLine();
                while (line != null) {
                    request.append(line.trim());
                    line = br.readLine();
                }
            } finally {
                br.close();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return request.toString();
    }

    public static InputStream getFileInputStreamByClassLocation(Class class1, String path) {
        InputStream in = class1.getClassLoader().getResourceAsStream(path);//"request_queryjsp.xml"
        return in;
    }

    public static Long download4System(InputStream fis, OutputStream out)
            throws IOException {
        Long length = 0L;
        try {
            length = (long) fis.available();
            byte bufferArray[] = new byte[10240];
            if (fis != null) {
                int byteLength = fis.read(bufferArray);
                while (byteLength != -1) {
                    out.write(bufferArray, 0, byteLength);
                    byteLength = fis.read(bufferArray);
                }
            }
            out.flush();
            out.close();

        } catch (Exception e) {
            //e.printStackTrace();
            log.error("文件下载失败！");
        } finally {
            // 使用IO包关闭流
            safeClose(out);
        }
        return length;
    }

    public static String toUtf8String(String s) {
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < s.length(); i++) {
            char c = s.charAt(i);
            if (c >= 0 && c <= 255) {
                sb.append(c);
            } else {
                byte[] b;
                try {
                    b = Character.toString(c).getBytes("utf-8");
                } catch (Exception ex) {
                    System.out.println(ex);
                    b = new byte[0];
                }
                for (int j = 0; j < b.length; j++) {
                    int k = b[j];
                    if (k < 0)
                        k += 256;
                    sb.append("%" + Integer.toHexString(k).toUpperCase());
                }
            }
        }
        return sb.toString();
    }

    /**
     * 利用BufferedReader实现Inputstream转换成String <功能详细描述>
     *
     * @param in
     * @return String
     */

    public static String inputstr2StrReader(InputStream in, String encode) {

        String str = "";
        try {
            if (encode == null || encode.equals("")) {
                // 默认以utf-8形式
                encode = "utf-8";
            }
            BufferedReader reader = new BufferedReader(new InputStreamReader(in, encode));
            StringBuffer sb = new StringBuffer();

            while ((str = reader.readLine()) != null) {
                sb.append(str).append("\n");
            }
            return sb.toString();
        } catch (UnsupportedEncodingException e1) {
            e1.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        return str;
    }

    /**
     * 利用byte数组转换InputStream------->String <功能详细描述>
     *
     * @param in
     * @return
     * @see [类、类#方法、类#成员]
     */

    public static String inputstr2StrByteArr(InputStream in, String encode) {
        StringBuffer sb = new StringBuffer();
        byte[] b = new byte[1024];
        int len = 0;
        try {
            if (encode == null || encode.equals("")) {
                // 默认以utf-8形式
                encode = "utf-8";
            }
            while ((len = in.read(b)) != -1) {
                sb.append(new String(b, 0, len, encode));
            }
            return sb.toString();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "";

    }

    /**
     * 利用ByteArrayOutputStream：Inputstream------------>String <功能详细描述>
     *
     * @param in
     * @return
     * @see [类、类#方法、类#成员]
     */
    public static String inputstr2StrByteArrayOutputStream(InputStream in, String encode) {
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        byte[] b = new byte[1024];
        int len = 0;
        try {
            if (encode == null || encode.equals("")) {
                // 默认以utf-8形式
                encode = "utf-8";
            }
            while ((len = in.read(b)) > 0) {
                out.write(b, 0, len);
            }
            return out.toString(encode);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "";
    }

    /**
     * 利用ByteArrayInputStream：String------------------>InputStream <功能详细描述>
     *
     * @param inStr
     * @return
     * @see [类、类#方法、类#成员]
     */
    public static InputStream str2Inputstr(String inStr) {
        try {
            // return new ByteArrayInputStream(inStr.getBytes());
            // return new ByteArrayInputStream(inStr.getBytes("UTF-8"));
            return new StringBufferInputStream(inStr);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
//	public static List<DefatulFile> getFilesByFolderPath(String dir){
//		List<DefatulFile> nameList = new ArrayList<DefatulFile>();
//		if (StringUtils.isEmpty(dir)) {
//			return nameList;
//		}
//		File folder = new File(dir);
//
////		File[] files = file.listFiles(new FileFilter() {
////			public boolean accept(File tmpFile) {
////				if (!tmpFile.isDirectory()) {
////					return true;
////				}
////				return false;
////			}
////		});
//		if(!folder.exists()){
//			throw new BusinessException("目录:"+dir+"不存在");
//		}
//		File[] files=folder.listFiles();
//		if(files==null){
//			throw new BusinessException("路径不能是文件");
//		}
//		for (File file:files) {
//			DefatulFile defatulFile=new DefatulFile();
//			defatulFile.setFileName(file.getName());
//			defatulFile.setFilePath(file.getAbsolutePath());
//			if(file.isDirectory()){
//				defatulFile.setFileType(DefatulFile.FOLDER);
//			}else{
//				defatulFile.setFileType(DefatulFile.FILE);
//				defatulFile.setFileSize(file.length());
//			}
//			nameList.add(defatulFile);
//		}
//
//		return nameList;
//	}
}

class ExtensionFileFilter implements FileFilter {

    private String extension;

    public ExtensionFileFilter(String extension) {
        this.extension = extension;
    }

    public File[] getFiles(String srcDir) throws IOException {
        return (File[]) FileUtils.listFiles(srcDir).toArray();
    }

    public boolean accept(File file) {
        if (file.isDirectory()) {
            return false;
        }

        String name = file.getName();
        // find the last
        int idx = name.lastIndexOf(".");
        if (idx == -1) {
            return false;
        } else if (idx == name.length() - 1) {
            return false;
        } else {
            return this.extension.equals(name.substring(idx + 1));
        }
    }
}
